package com.zrrd.network.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author WanJl
 * @version 1.0
 * @title ReceiveDemo03
 * @description 广播接收
 * @create 2023/10/22 11:29
 */
public class ReceiveDemo03 {
    public static void receive( int port) throws IOException {
        //1. 创建接收端的Socket对象(DatagramSocket)
        DatagramSocket ds=new DatagramSocket(port);
        //2. 创建一个数据包，用于接收数据
        DatagramPacket dp=new DatagramPacket(new byte[1024],1024);
        //3. 调用DatagramSocket对象的方法接收数据
        ds.receive(dp);
        //4. 解析数据包，并把数据在控制台显示
        String s = new String(dp.getData(), 0, dp.getLength());
        System.out.println("接收到广播数据："+s);
        //6、释放资源
        ds.close();
    }

    public static void main(String[] args) throws IOException {
        receive(10011);
    }
}
